package com.suitsoft.ut.dao.base;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.base.Discipline;

/**
 *
 *@author Suineg
 */
@Repository
public class DisciplineDao extends HibernateDao<Discipline,Long>{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 获取学院的所有专业
	 * @param code 学院的IDS
	 * @return
	 */
	public List<Discipline> findByCollege(String code){
		String hql = "from Discipline as d inner join fetch d.college as c" +
				" where c.id in (" +code+ ")";
		
		return find(hql);
	}
	
	/**
	 * 判断是否存在 没就新建一个.
	 * @param college
	 * @param name
	 * @return
	 */
	public Discipline isHaveNoNew(College college,String name){
		Discipline discipline = null;
		for(Discipline d : college.getDiscipline()){
			if(name.trim().equals(d.getDisciplineName().trim())){
				discipline = d;
				break;
			}
		}
		if(discipline == null){
			User user = new User();
			user.setId(UserHolder.getLoginUser().getId());
			
			discipline = new Discipline();
			discipline.setCollege(college);
			discipline.setDisciplineName(name);
			save(discipline);
			flush();
			college.getDiscipline().add(discipline);
		}
		return discipline;
	}
	
	
	public void save(Discipline discipline){
		User user = new User();
		user.setId(UserHolder.getLoginUser().getId());
		
		discipline.setLastUser(user);
		discipline.setOpdate(new Date());
		super.save(discipline);
	}

}

